package com.hai.qtalk.service;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.hai.qtalk.service.FriendshipService;
import com.hai.qtalk.service.GroupService;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;
import com.hai.qtalk.base.BaseDAO;
import com.hai.qtalk.bean.*;

/**
 * 用户相关的业务逻辑类
 * 用户登录，注册，修改资料，查找其他用户，添加好友，删除好友，拉黑其他用户
 * @author xinhai
 * @version 1.0
 */
public class UserService{

	private BaseDAO<User> uDao = new BaseDAO<User>(); 
	
	
	
	/**
	 * 用户注册
	 * @param user
	 */
	public long addUser(User user){
		return uDao.save(user);
	}
	
	/**
	 * 用户登陆
	 * @param userId
	 * @param pwd
	 * @return json
	 */
	public JSONObject login(int userId,String pwd){
		//User u=uDao.find(User.class, userId);
		User u=uDao.find("select u from User u where u.UId ='"+userId+"'");
		if(u!=null&&u.getUPassword().equals(pwd)){
			//List info = new ArrayList();
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("user", u);
			List<Group> groupList= GroupService.findAllGroup(userId);
			map.put("groupList", groupList);
			List<Friendship> friends = FriendshipService.findAllFriendship(userId);
			map.put("friends", friends);
			JsonConfig jsonConfig=new JsonConfig();
			jsonConfig.setIgnoreDefaultExcludes(false);
			jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
			jsonConfig.setExcludes(new String[]{"friendships","groups","messagesForMSender","friendshipsForHostId","messagesForMReceiver","friendshipsForFriendId"});
			JSONObject json = JSONObject.fromObject(map,jsonConfig);
			//System.out.println(json.toString());
			return json;
		}
		System.out.println("用户"+userId+"登录密码错误!");
		return null;
	}
	
	/**
	 * 用户登陆
	 * @param userId
	 * @param pwd
	 * @return json
	 */
	public JSONObject login(String userId,String pwd){
		int id = Integer.parseInt(userId);
		return login(id,pwd);
	}
	
	/**
	 * 修改用户资料
	 * @param user
	 */
	public void updateUser(User user){
		uDao.update(user);
	}
	
	/**
	 * 添加好友
	 * @param user
	 * @param friend
	 * @param remark
	 * @param GName
	 */
	public void addFriend(User user,User friend,String remark,String GName){
		Group g=new Group(user,GName);
		Friendship f=new Friendship(g,friend,user,remark);
		FriendshipService.addFriend(f);
	}
	
	/**
	 * 删除好友
	 * @param user
	 * @param friend
	 */
	public void deleteFriend(User host,User friend){
		Friendship f=FriendshipService.findFriendship(host.getUId(), friend.getUId());
		FriendshipService.deleteFriend(f);
	}

	/**
	 * 根据用户ID查找用户
	 * @param userId
	 * @return
	 */
	public User findFriendById(int userId){
//		System.out.println("This is println message.");  
//		Logger logger = Logger.getLogger(UserService.class);
//        // 记录debug级别的信息  
//        logger.debug("This is debug message.");  
//        // 记录info级别的信息  
//        logger.info("This is info message.");  
//        // 记录error级别的信息  
//        logger.error("This is error message.");  
		return uDao.find("select u from User u Where u.UId ='"+userId+"'");
	}
	
	
	/**
	 * 根据用户真实姓名或昵称查找用户
	 * @param name
	 * @return
	 */
	public List<User> findFriendByName(String name){
		return uDao.list("select u from User u Where u.UName ='"+name+"'or u.UNickname ='"+name+"'");
	}
	
	/**
	 * 拉黑其它用户
	 * @param host
	 * @param friend
	 */
	public void black(User host,User friend){
		Friendship f=FriendshipService.findFriendship(host.getUId(), friend.getUId());
		f.setGroup(null);//null表示黑名单组
	}
}
